<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'dashboard';
	protected $_ci;
	protected $_currentUserGroup;
	protected $_currentUserName = '';
	protected $_curentUserZones;
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('dashboard');
		$this->_currentUserGroup = $this->_ci->ion_auth->get_user()->group;
		$this->_currentUserName = $this->_ci->ion_auth->get_user()->username;
		$this->_curentUserZones = $this->_ci->escort_user_roles_n_zones_entity->getUserRolesNZones($this->_currentUserName);
		
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
		
						->append_metadata(js('https://www.google.com/jsapi'))
						->append_metadata(js('escort.dashboard.js', 'dashboard'))
						->append_metadata(css('escort.dashboard.css', 'dashboard'));
	}

	/**
	 * List all items
	 */
	public function index()
	{	
				
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$escortIdList = $this->_ci->user_entity->getEscortIdListByAdmin($this->_currentUserName, $this->_currentUserGroup , $this->_curentUserZones);
		if($escortIdList && count($escortIdList)){
			$unreadMessages = $this->_ci->escort_message_thread_entity->countMessageThreadByEscortIdList($escortIdList, $this->_currentUserGroup, true);
		}else{
			$unreadMessages = 0;
		}
		
		switch ($this->_currentUserGroup){
			case USER_TYPE_INVOICER:
				$activeDiscount = $this->discount_ticket_entity->countActiveDiscount();
				$completedObjective = $this->commercial_objective_entity->countCompletedObjective();
				$numberOfUser = $this->user_entity->countUserList();
				$activeVolumeDiscount = $this->volume_discount_entity->countActiveVolumeDiscount();
				$expenseOfMonth = $this->_ci->expense_entity->getExpenseOfMonth();
				$activeInvoice = $this->_ci->invoice_entity->countActiveInvoice();
				$pendingWithdrawal = $this->_ci->withdrawal_entity->countPendingWithdrawal();
				
				$this->template
					->title($this->module_details['name'])
					->append_metadata(js('admin/filter.js'))
					->set('unreadMessages', $unreadMessages)
					->set('activeDiscount', $activeDiscount)
					->set('completedObjective', $completedObjective)
					->set('numberOfUser', $numberOfUser)
					->set('activeVolumeDiscount', $activeVolumeDiscount)
					->set('expenseOfMonth', $expenseOfMonth)
					->set('activeInvoice', $activeInvoice)
					->set('pendingWithdrawal', $pendingWithdrawal)
					->set('userGroup', $this->_currentUserGroup);
				$this->template->build('admin/invoicer', $this->data);
					
				break;
			case USER_TYPE_QUALITY_CONTROL:
				if($escortIdList && count($escortIdList)){
					$pendingReviewEscort = $this->_ci->escort_entity->countPendingReviewEscort($escortIdList);
					$abuseNotificationEscort = $this->_ci->escort_entity->countAbuseNotificationEscort($escortIdList);
					$unReviewPhoto = $this->_ci->media_entity->countMediaListByEscortIdList($escortIdList, true);
				}else{
					$pendingReviewEscort = 0;
					$abuseNotificationEscort = 0;
					$unReviewPhoto = 0;
				}
				$this->template
					->title($this->module_details['name'])
					->append_metadata(js('admin/filter.js'))
					->set('unreadMessages', $unreadMessages)
					->set('pendingReviewEscort', $pendingReviewEscort)
					->set('unReviewPhoto', $unReviewPhoto)
					->set('abuseNotificationEscort', $abuseNotificationEscort)
					->set('userGroup', $this->_currentUserGroup);
				$this->template->build('admin/quanlity-control', $this->data);
					
				break;
			case USER_COUNTRY_MANAGER:
				if(count($userZonesCountryIdList) > 0){
					$userList = $this->_ci->user_entity->getUserListByCountry($userZonesCountryIdList);
				}else{
					$userList = $this->_ci->user_entity->getAllUser();
				}
				$userIdList = array();
				if($userList){
					foreach ($userList as $user) {
						$userIdList[] = $user->id;
					}
				}
				if(count($userIdList) > 0){
					$total_rows = $this->_ci->escort_entity->countEscortByUser($userIdList);
					$pagination = create_pagination('admin/dashboard/index', $total_rows);
					$escortList = $this->_ci->escort_entity->getEscortListByUser($userIdList, $pagination['limit']);
				}else{
					$pagination = create_pagination('admin/dashboard/index', 0);
					$escortList = null;
				}
				if($escortList){
					foreach($escortList as $escort){
						$escort->userInfo = $this->_ci->user_entity->init('id', $escort->user_id);
					}
				}
				$this->template
					->title($this->module_details['name'])
					->append_metadata(js('admin/filter.js'))
					->set('pagination', $pagination)
					->set('escortList', $escortList)
					->set('unreadMessages', $unreadMessages)
					->set('userGroup', $userGroup);
				$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/country-manager', $this->data);
				break;
			case 'agent':
				break;
			case 'province-manager':
				break;
			case 'city-manager':
				break;
			case 'commercial':
				break;
			case 'support-level-1':
				break;
			case 'support-level-2':
				break;
			default:
				if($escortIdList && count($escortIdList)){
					$pendingReviewEscort = $this->_ci->escort_entity->countPendingReviewEscort($escortIdList);
					$abuseNotificationEscort = $this->_ci->escort_entity->countAbuseNotificationEscort($escortIdList);
					$unReviewPhoto = $this->_ci->media_entity->countMediaListByEscortIdList($escortIdList, true);
				}else{
					$pendingReviewEscort = 0;
					$abuseNotificationEscort = 0;
					$unReviewPhoto = 0;
				}
				$this->template
					->title($this->module_details['name'])
					->append_metadata(js('admin/filter.js'))
					->set('unreadMessages', $unreadMessages)
					->set('pendingReviewEscort', $pendingReviewEscort)
					->set('unReviewPhoto', $unReviewPhoto)
					->set('abuseNotificationEscort', $abuseNotificationEscort)
					->set('userGroup', $this->_currentUserGroup);
				$this->template->build('admin/quanlity-control', $this->data);
					
				break;
		}		
	}
	public function invoice_data(){
		$fromDate = $this->input->post('from_date');
		$toDate = $this->input->post('to_date');
		$timeData = calculateTimeBetweenTwoDay($fromDate, $toDate);
		$result = array();
		$result['title'] = lang('escort_dashboard_invoices_statistic_label');
		$result['y'] = lang('escort_dashboard_update_invoices_chart_money_label');
		$result['time'] = $timeData;
		$invoiceData = array();
		if($timeData['years'] > 2){
		$result['x'] = lang('escort_dashboard_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffYears']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($fromDate)));
				}else{
					$nexYear = format_date(strtotime('+'.$i.' year', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($nexYear)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$invoiceData[] = array(date('Y',strtotime($fromDate)), $money);
				}else{
					$invoiceData[] = array(date('Y',strtotime($nexYear)), $money);
				}
			}			
		}else if($timeData['diffDays'] > 31){
			$result['x'] = lang('escort_dashboard_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffMonths']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($fromDate)), date('y',strtotime($fromDate)));
				}else{
					$nexMonth = format_date(strtotime('+'.$i.' month', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($nexMonth)), date('y',strtotime($nexMonth)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$invoiceData[] = array(date('m/Y',strtotime($fromDate)), $money);
				}else{
					$invoiceData[] = array(date('m/Y',strtotime($nexMonth)), $money);
				}
			}
		}else{
			$result['x'] = lang('escort_dashboard_update_invoices_chart_date_label');
			for($i = 0; $i < $timeData['diffDays']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay($fromDate);
				}else{
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay(format_date(strtotime('+'.$i.' day', strtotime($fromDate))));	
				}
				
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$invoiceData[] = array($fromDate, $money);
				}else{
					$invoiceData[] = array(format_date(strtotime('+'.$i.' day', strtotime($fromDate))), $money);
				}
			}
		}
		$result['data'] = $invoiceData;
		echo json_encode($result);
	}
}
